﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class CitationParserInstruction:InstructionBase
    {
        #region props
        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _SQL;
        /// <summary></summary>
        public string SQL
        {
            get
            {
                return this._SQL;
            }
            set
            {
                this._SQL = value;
            }
        }

        private string _KeyField;
        /// <summary></summary>
        public string KeyField
        {
            get
            {
                return this._KeyField;
            }
            set
            {
                this._KeyField = value;
            }
        }

        private string _CitationTimeField;
        /// <summary></summary>
        public string CitationTimeField
        {
            get
            {
                return this._CitationTimeField;
            }
            set
            {
                this._CitationTimeField = value;
            }
        }

        private string _CitationField;
        /// <summary></summary>
        public string CitationField
        {
            get
            {
                return this._CitationField;
            }
            set
            {
                this._CitationField = value;
            }
        }

        private string _OutputTable;
        /// <summary></summary>
        public string OutputTable
        {
            get
            {
                return this._OutputTable;
            }
            set
            {
                this._OutputTable = value;
            }
        }

        private string _SrcKeyField;
        /// <summary></summary>
        public string SrcKeyField
        {
            get
            {
                return this._SrcKeyField;
            }
            set
            {
                this._SrcKeyField = value;
            }
        }

        private string _PubTimeField;
        /// <summary></summary>
        public string PubTimeField
        {
            get
            {
                return this._PubTimeField;
            }
            set
            {
                this._PubTimeField = value;
            }
        }

        private string _TitleField;
        /// <summary></summary>
        public string TitleField
        {
            get
            {
                return this._TitleField;
            }
            set
            {
                this._TitleField = value;
            }
        }

        private string _BookTitleMeetingField;
        /// <summary></summary>
        public string BookTitleMeetingField
        {
            get
            {
                return this._BookTitleMeetingField;
            }
            set
            {
                this._BookTitleMeetingField = value;
            }
        }

        private string _AuthorsField;
        /// <summary></summary>
        public string AuthorsField
        {
            get
            {
                return this._AuthorsField;
            }
            set
            {
                this._AuthorsField = value;
            }
        }

        private string _JournalNameField;
        /// <summary></summary>
        public string JournalNameField
        {
            get
            {
                return this._JournalNameField;
            }
            set
            {
                this._JournalNameField = value;
            }
        }

        private string _VolumeField;
        /// <summary></summary>
        public string VolumeField
        {
            get
            {
                return this._VolumeField;
            }
            set
            {
                this._VolumeField = value;
            }
        }

        private string _IssueField;
        /// <summary></summary>
        public string IssueField
        {
            get
            {
                return this._IssueField;
            }
            set
            {
                this._IssueField = value;
            }
        }

        private string _PageField;
        /// <summary></summary>
        public string PageField
        {
            get
            {
                return this._PageField;
            }
            set
            {
                this._PageField = value;
            }
        }

        private string _WosItemIDField;
        /// <summary></summary>
        public string WosItemIDField
        {
            get
            {
                return this._WosItemIDField;
            }
            set
            {
                this._WosItemIDField = value;
            }
        }

        private CitationParserAlgorithms _Algorithm;
        /// <summary></summary>
        public CitationParserAlgorithms Algorithm
        {
            get
            {
                return this._Algorithm;
            }
            set
            {
                this._Algorithm = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public CitationParserInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._ConnStr = string.Empty;
            this._SQL = string.Empty;
            this._KeyField = string.Empty;
            this._CitationTimeField = string.Empty;
            this._CitationField = string.Empty;

            this._OutputTable = string.Empty;
            this._SrcKeyField = string.Empty;
            this._PubTimeField = string.Empty;
            this._TitleField = string.Empty;
            this._BookTitleMeetingField = string.Empty;
            this._AuthorsField = string.Empty;
            this._JournalNameField = string.Empty;
            this._VolumeField = string.Empty;
            this._IssueField = string.Empty;
            this._PageField = string.Empty;
            this._WosItemIDField = string.Empty;
            this._Algorithm = CitationParserAlgorithms.DecisionTree;
        }

        /// <summary>
        /// constructor, all derived class must call default constructor to initialize
        /// default settings.
        /// </summary>
        public CitationParserInstruction(Guid componentID, Guid jobTicketID,
            string connStr, string sql, string keyField, string citeTimeField, string citeTextField,
            string outputTable, string srcKeyField, string pubTimeField,
            string titleField, string bookTitleMeeting, string authorsField, 
            string journalField, string volField, 
            string issueField, string pageField,
            string wosItemField, CitationParserAlgorithms algorithm) 
            : base(componentID, jobTicketID)
        {
            this._ConnStr = connStr;
            this._SQL = sql;
            this._KeyField = keyField;
            this._CitationTimeField = citeTimeField;
            this._CitationField = citeTextField;
            this._OutputTable = outputTable;
            this._SrcKeyField = srcKeyField;
            this._PubTimeField = pubTimeField;
            this._TitleField = titleField;
            this._BookTitleMeetingField = bookTitleMeeting;
            this._AuthorsField = authorsField;
            this._JournalNameField = journalField;
            this._VolumeField = volField;
            this._IssueField = issueField;
            this._PageField = pageField;
            this._WosItemIDField = wosItemField;
            this._Algorithm = algorithm;
        }
        #endregion

        #region Overrides of InstructionBase

        /// <summary>
        /// serialize settings to xml
        /// the function below is mainly for storing into database as serialized form instead of string
        /// </summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SQL", this.SQL);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "KeyField", this.KeyField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CitationTimeField", this.CitationTimeField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CitationField", this.CitationField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "OutputTable", this.OutputTable);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcKeyField", this.SrcKeyField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PubTimeField", this.PubTimeField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TitleField", this.TitleField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BookTitleMeeting", this.BookTitleMeetingField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "AuthorsField", this.AuthorsField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "JournalField", this.JournalNameField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "VolumeField", this.VolumeField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IssueField", this.IssueField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "PageField", this.PageField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WosItemField", this.WosItemIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Algorithm", this.Algorithm.ToString());

            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CitationParserInstruction instruction = new CitationParserInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                instruction.SQL = XmlDataUtil.GetAttributeValue(xNode, "SQL", "");
                instruction.KeyField = XmlDataUtil.GetAttributeValue(xNode, "KeyField", "");
                instruction.CitationTimeField = XmlDataUtil.GetAttributeValue(xNode, "CitationTimeField", "");
                instruction.CitationField = XmlDataUtil.GetAttributeValue(xNode, "CitationField", "");
                instruction.OutputTable = XmlDataUtil.GetAttributeValue(xNode, "OutputTable", "");
                instruction.SrcKeyField = XmlDataUtil.GetAttributeValue(xNode, "SrcKeyField", "");
                instruction.PubTimeField = XmlDataUtil.GetAttributeValue(xNode, "PubTimeField", "");
                instruction.TitleField = XmlDataUtil.GetAttributeValue(xNode, "TitleField", "");
                instruction.BookTitleMeetingField = XmlDataUtil.GetAttributeValue(xNode, "BookTitleMeeting", "");
                instruction.AuthorsField = XmlDataUtil.GetAttributeValue(xNode, "AuthorsField", "");
                instruction.JournalNameField = XmlDataUtil.GetAttributeValue(xNode, "JournalField", "");
                instruction.VolumeField = XmlDataUtil.GetAttributeValue(xNode, "VolumeField", "");
                instruction.IssueField = XmlDataUtil.GetAttributeValue(xNode, "IssueField", "");
                instruction.PageField = XmlDataUtil.GetAttributeValue(xNode, "PageField", "");
                instruction.WosItemIDField = XmlDataUtil.GetAttributeValue(xNode, "WosItemField", "");
                instruction.Algorithm =
                    (CitationParserAlgorithms)
                    Enum.Parse(
                        typeof (CitationParserAlgorithms),
                        XmlDataUtil.GetAttributeValue(
                            xNode, "Algorithm",
                            CitationParserAlgorithms.DecisionTree.ToString()));

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
